package com.learning.hadoop.hdfs;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

/**
 * ClassName: Test
 * Description:
 * Date: 2018/3/22 17:42 【需求编号】
 *
 * @author Sam Sho
 * @version V1.0.0
 */
public class HDFSTest {

    public static void main(String[] args) throws IOException {
        String url = "hdfs://newlistest02:9000";
        URI uri = URI.create(url);
        // 创建Configuration对象
        Configuration conf = new Configuration();
        // 创建FileSystem对象
        FileSystem fs = FileSystem.get(uri, conf);

        Path path = new Path("/wordcount.txt");
        FSDataInputStream is = fs.open(path);
        byte[] buff = new byte[1024];
        int length = 0;
        while ((length = is.read(buff)) != -1) {
            System.out.println(new String(buff, 0, length));
        }

        fs.close();
    }

    public void Catalog() throws Exception {

        String url = "hdfs://newlistest02:9000";
        URI uri = URI.create(url);
        // 创建Configuration对象
        Configuration conf = new Configuration();
        // 创建FileSystem对象
        FileSystem fileSystem = FileSystem.get(uri, conf);

        Path path = new Path("/");
        FileStatus fileStatus = fileSystem.getFileStatus(path);
        System.out.println("*************************************");
        System.out.println("文件根目录: " + fileStatus.getPath());
        System.out.println("这文件目录为：");
        for (FileStatus fs : fileSystem.listStatus(path)) {
            System.out.println(fs.getPath());
        }
    }

    /**
     * 上传文件
     */
    public void upload() throws Exception {
        String url = "hdfs://newlistest02:9000";
        URI uri = URI.create(url);
        // 创建Configuration对象
        Configuration conf = new Configuration();
        // 创建FileSystem对象
        FileSystem fileSystem = FileSystem.get(uri, conf);


        Path srcPath = new Path("C:/Users/Administrator/Desktop/hadoop/hadoop.txt");
        Path dstPath = new Path("/");
        fileSystem.copyFromLocalFile(false, srcPath, dstPath);
        fileSystem.close();
        System.out.println("*************************************");
        System.out.println("上传成功！");
    }

    /**
     * 下载文件
     */
    public void download() throws Exception {
        String url = "hdfs://newlistest02:9000";
        URI uri = URI.create(url);
        // 创建Configuration对象
        Configuration conf = new Configuration();
        // 创建FileSystem对象
        FileSystem fileSystem = FileSystem.get(uri, conf);

        InputStream in = fileSystem.open(new Path("/hadoop.txt"));
        OutputStream out = new FileOutputStream("E://hadoop.txt");
        IOUtils.copyBytes(in, out, 4096, true);
    }
}
